/*------------------------------------------------------------------------------
#
#	Apple Macintosh Developer Technical Support
#
#	MultiFinder-Aware Simple TextEdit Sample Application
#
#	CPlusTESample
#
#	TDocument.cp	-	C++ source
#
#	Copyright © 1989 Apple Computer, Inc.
#	All rights reserved.
#
#	Versions:	
#			1.00 					04/89
#
#	Components:
#			CPlusTESample.make		April 1, 1989
#			TApplicationCommon.h	April 1, 1989
#			TApplication.h			April 1, 1989
#			TDocument.h				April 1, 1989
#			TECommon.h				April 1, 1989
#			TESample.h				April 1, 1989
#			TEDocument.h			April 1, 1989
#			TApplication.cp			April 1, 1989
#			TDocument.cp			April 1, 1989
#			TESample.cp				April 1, 1989
#			TEDocument.cp			April 1, 1989
#			TESampleGlue.a			April 1, 1989
#			TApplication.r			April 1, 1989
#			TESample.r				April 1, 1989
#
#	CPlusTESample is an example application that demonstrates
#	how to initialize the commonly used toolbox managers,
#	operate successfully under MultiFinder, handle desk
#	accessories and create, grow, and zoom windows. The
#	fundamental TextEdit toolbox calls and TextEdit autoscroll
#	are demonstrated. It also shows how to create and maintain
#	scrollbar controls. 
#
#	This version of TESample has been substantially reworked in
#	C++ to show how a "typical" object oriented program could
#	be written. To this end, what was once a single source code
#	file has been restructured into a set of classes which
#	demonstrate the advantages of object-oriented programming.
#
------------------------------------------------------------------------------*/

/*
Segmentation strategy:

    This program has only one segment, since the issues
    surrounding segmentation within a class's methods have
    not been investigated yet. We DO unload the data
    initialization segment at startup time, which frees up
    some memory 

SetPort strategy:

    Toolbox routines do not change the current port. In
    spite of this, in this program we use a strategy of
    calling SetPort whenever we want to draw or make calls
    which depend on the current port. This makes us less
    vulnerable to bugs in other software which might alter
    the current port (such as the bug (feature?) in many
    desk accessories which change the port on OpenDeskAcc).
    Hopefully, this also makes the routines from this
    program more self-contained, since they don't depend on
    the current port setting. 

Clipboard strategy:

    This program does not maintain a private scrap.
    Whenever a cut, copy, or paste occurs, we import/export
    from the public scrap to TextEdit's scrap right away,
    using the TEToScrap and TEFromScrap routines. If we did
    use a private scrap, the import/export would be in the
    activate/deactivate event and suspend/resume event
    routines. 
*/

// Mac Includes
#include <Types.h>
#include <QuickDraw.h>
#include <Fonts.h>
#include <Events.h>
#include <Controls.h>
#include <Windows.h>
#include <Menus.h>
#include <TextEdit.h>
#include <Dialogs.h>
#include <Desk.h>
#include <Scrap.h>
#include <ToolUtils.h>
#include <Memory.h>
#include <SegLoad.h>
#include <Files.h>
#include <OSUtils.h>
#include <Traps.h>

#include "TDocument.h"

TDocument::TDocument(short resID)
{
	fDocWindow = GetNewWindow(resID,nil,(WindowPtr) -1);
	SetPort(fDocWindow);
}

TDocument::~TDocument(void)
{
	DisposeWindow(fDocWindow);
}

TDocumentLink::TDocumentLink(TDocumentLink *n, TDocument *v)
{
	fNext = n;
	fDoc = v;
}

TDocumentList::TDocumentList(void)
{
	fDocList = nil;
	fNumDocs = 0;
}

// find the TDocument associated with the window
TDocument* TDocumentList::FindDoc(WindowPtr window)
{
	TDocumentLink* temp;
	TDocument* tDoc;

	for (temp = fDocList; temp != nil; temp = temp->GetNext())
	  {
		tDoc = temp->GetDoc();
		if (tDoc->GetDocWindow() == window)
		  return tDoc;
	  }
	return nil;
}

// private list management routines
void TDocumentList::AddDoc(TDocument* doc)
{
	TDocumentLink* temp;

	temp = new TDocumentLink(fDocList,doc);
	fDocList = temp;
	fNumDocs++;
}

void TDocumentList::RemoveDoc(TDocument* doc)
{
	TDocumentLink* temp;
	TDocumentLink* last;

	last = nil;
	for (temp = fDocList; temp != nil; temp = temp->GetNext())
	  if (temp->GetDoc() == doc)
	    {
		  if (last == nil)				// if first item in list, just set first
			fDocList = temp->GetNext();
		  else last->SetNext(temp->GetNext());
		  delete temp;					// free the TDocumentLink
		  fNumDocs--;
		  return;
		}
	  else last = temp;
}
